//app.js
App({
  onLaunch: function (opt) {
    if (opt.query.fWid) {
      this.globalData.fWid = opt.query.fWid
    } else {
      this.globalData.fWid = false
    }
    if (opt.query.fenxiang) {
      this.globalData.fOpenid = opt.query.fenxiang
    } else {
      this.globalData.fOpenid = false
    }
    let that = this;
    //that.getOpenid();
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        if (res.data != undefined) {
          that.globalData.openid = res.data;
          wx.getStorage({
            key: 'userInfo',
            success: function (res) {
              that.globalData.userInfo = res.data
            }
          })
        } else {
          that.getOpenid();
        }
      },
      fail: function (res) {
        that.getOpenid();
      }
    })
    that.ihttpGet('https://www.58zltc.com/api.php/Index/getGlobaWid',function (res) {
      that.globalData.wid = res.data;
    })
  },
  getUserInfo: function(){
    // 获取用户信息
    let that = this;
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        if (res.data != undefined) {
          that.globalData.openid = res.data;
          wx.getStorage({
            key: 'userInfo',
            success: function (res) {
              that.globalData.userInfo = res.data
            }
          })
        } else {
          that.getOpenid();
        }
      },
      fail: function (res) {
        that.getOpenid();
      }
    })
  },
  getOpenid: function () {
    let that = this;
    wx.login({
      success: res => {
        that.ihttpPost("https://www.58zltc.com/api.php/Index/login", { code: res.code },
          function (res) {
            if (res.data.err_code > 0) {
              that.getOpenid();
            } else {
              //老用户从数据库读数据
              that.globalData.openid = res.data.openid
              if (res.data.code == 1) {
                wx.setStorage({
                  key: 'openid',
                  data: res.data.openid,
                })
                wx.setStorage({
                  key: 'userInfo',
                  data: res.data.user,
                })
                that.globalData.userInfo = res.data.user
              } else {
                //新用户获取存入数据库(用button按钮)
                wx.navigateTo({
                  url: '/pages/index/index/login',
                })
                //that.getUserInfo();
              }
            }
          });
      }, fail: function (res) {
        
      }
    }
    )
  },
  getUserInfo1: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  ihttpPost: function (url, postData, doSuccess, doFail){
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.request({
      url: url + '?token=' + token,
      method: 'POST',
      data: postData,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res=>{
        doSuccess(res);
      }
    })
  },
  ihttpGet: function (url, doSuccess, doFail) {
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.request({
      url: url + '?token=' + token,
      success: res => {
        doSuccess(res);
        wx.hideLoading();
      }
    })
  },
  ihttpGetOutLoading: function (url, doSuccess, doFail) {
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.request({
      url: url + '?token=' + token,
      success: res => {
        doSuccess(res);
      }
    })
  },
  iModel: function(content){
    wx.showModal({
      title: '提示',
      content: content,
      showCancel: false,
      confirmText: '我知道了'
    })
  },
  addFormId: function(formId){
    this.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: formId, openid: this.globalData.openid}, function(res){});
  },
  globalData: {
    appid:null,
    userInfo: null,
    openid: null,
  },
  //确认分销关系
  confirmCommission: function () {
    let that = this
    var fWidG = this.globalData.fWid
    var fOpenidG = this.globalData.fOpenid
    if (fWidG && fOpenidG) {
      var myOpenid = that.globalData.openid
      if (fOpenidG != myOpenid) {
        that.ihttpGet("https://www.58zltc.com/api.php/Index/createGrade/fOpenid/" + fOpenidG + '/myOpenid/' + myOpenid + '/fWid/' + fWidG, function (res) { })
      }
    }
  }
})